package cn.jji8.patternLogin.spigot.login;

import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.Plugin;

import java.util.Random;

/**
 * 管理用于登录的世界
 * */
public class LoginWorld {
    static String wordName = null;
    static World loginWord = null;
    /**
     * 插件启动时被调用，在main的启动方法中
     * */
    @SuppressWarnings("unused")
    public static void load(Plugin plugin){
        if(wordName==null){
            wordName = "patternLoginWorld";
        }
        WorldCreator worldCreator = new WorldCreator(wordName);
        worldCreator.type(WorldType.FLAT);
        worldCreator.generateStructures(false);
        //noinspection NullableProblems
        worldCreator.generator(new ChunkGenerator() {
            @Override
            public ChunkData generateChunkData(World world, Random random, int x, int z, BiomeGrid biomes) {
                return createChunkData(world);
            }
        });
        loginWord = Bukkit.createWorld(worldCreator);

        //如果是第一次加载就构造平台
        if(Configuring.first){
            Block block = new Location(loginWord,Configuring.playerLoginX,Configuring.playerLoginY-1,Configuring.playerLoginZ).getBlock();
            if(block.getType().equals(Material.AIR)){
                block.setType(Material.GLASS);
            }
            BuildingPlatform(new Location(loginWord,Configuring.LoginLocationX,Configuring.LoginLocationY,Configuring.LoginLocationZ));
        }
    }
    /**
     * 在指定位置构建登录平台
     * */
    public static void BuildingPlatform(Location location){
        new Location(location.getWorld(),location.getBlockX()+1,location.getBlockY()+3,location.getBlockZ()+1).getBlock().setType(Material.STONE_BUTTON);
        new Location(location.getWorld(),location.getBlockX()+2,location.getBlockY()+3,location.getBlockZ()+1).getBlock().setType(Material.STONE_BUTTON);
        new Location(location.getWorld(),location.getBlockX()+3,location.getBlockY()+3,location.getBlockZ()+1).getBlock().setType(Material.STONE_BUTTON);
        new Location(location.getWorld(),location.getBlockX()+1,location.getBlockY()+2,location.getBlockZ()+1).getBlock().setType(Material.STONE_BUTTON);
        new Location(location.getWorld(),location.getBlockX()+2,location.getBlockY()+2,location.getBlockZ()+1).getBlock().setType(Material.STONE_BUTTON);
        new Location(location.getWorld(),location.getBlockX()+3,location.getBlockY()+2,location.getBlockZ()+1).getBlock().setType(Material.STONE_BUTTON);
        new Location(location.getWorld(),location.getBlockX()+1,location.getBlockY()+1,location.getBlockZ()+1).getBlock().setType(Material.STONE_BUTTON);
        new Location(location.getWorld(),location.getBlockX()+2,location.getBlockY()+1,location.getBlockZ()+1).getBlock().setType(Material.STONE_BUTTON);
        new Location(location.getWorld(),location.getBlockX()+3,location.getBlockY()+1,location.getBlockZ()+1).getBlock().setType(Material.STONE_BUTTON);

        new Location(location.getWorld(),location.getBlockX()+1,location.getBlockY()+3,location.getBlockZ()+2).getBlock().setType(Material.GLASS);
        new Location(location.getWorld(),location.getBlockX()+2,location.getBlockY()+3,location.getBlockZ()+2).getBlock().setType(Material.GLASS);
        new Location(location.getWorld(),location.getBlockX()+3,location.getBlockY()+3,location.getBlockZ()+2).getBlock().setType(Material.GLASS);
        new Location(location.getWorld(),location.getBlockX()+1,location.getBlockY()+2,location.getBlockZ()+2).getBlock().setType(Material.GLASS);
        new Location(location.getWorld(),location.getBlockX()+2,location.getBlockY()+2,location.getBlockZ()+2).getBlock().setType(Material.GLASS);
        new Location(location.getWorld(),location.getBlockX()+3,location.getBlockY()+2,location.getBlockZ()+2).getBlock().setType(Material.GLASS);
        new Location(location.getWorld(),location.getBlockX()+1,location.getBlockY()+1,location.getBlockZ()+2).getBlock().setType(Material.GLASS);
        new Location(location.getWorld(),location.getBlockX()+2,location.getBlockY()+1,location.getBlockZ()+2).getBlock().setType(Material.GLASS);
        new Location(location.getWorld(),location.getBlockX()+3,location.getBlockY()+1,location.getBlockZ()+2).getBlock().setType(Material.GLASS);
    }
    /**
     * 获取loanword
     * */
    public static World getLoginWord(){
        return loginWord;
    }
}
